/*
 * Copyleft 2012 Power by tangjiannan.
 *
 * Project: crm
 * Date: 2012-5-20
 */
package com.crm.business.web.svcpkg.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.crm.business.web.svcpkg.service.SvcPkgService;
import com.crm.common.web.service.impl.WebCommonServiceImpl;

/**
 * 
 * 套餐,及套餐关联服务项接口实现类
 * 
 * @author tangjiannan
 * 
 */
public class SvcPkgServiceImpl extends WebCommonServiceImpl implements
        SvcPkgService {
    /** Logger */
    private static final Logger LOG = Logger
            .getLogger(SvcItemServiceImpl.class);

    /**
     * 查询套餐
     * 
     * @param params
     */
    public void doQuery(Map<String, Object> params) {
        // query
        List<Map<String, Object>> list = getCommonDao().namingQueryForList(
                "sql_svc_pkg_querySvcPkgByCond", params);

        LOG.info(list.size() + " items(s) fetched!");
        for (Map<String, Object> item : list) {
            item.put("status_disp_nm", getCdNmByValue("CD0007", item
                    .get("status")));
        }
        params.put("totalCount", list.size());
        params.put("svcitems", list);
    }

    /**
     * 更新套餐
     * 
     * @param params 参数
     * @return
     */
    public boolean doUpdatePkgs(Map<String, Object> params) {
        int num = getCommonDao().namingUpdate("sql_svc_pkg_updateSvcPkgByCond",
                params);
        LOG.info(num + " svcpkg(s)  updated!");
        return num > 0 ? true : false;
    }

    /**
     * 添加套餐
     * 
     * @param params
     * @return
     */
    public boolean doAddPkgs(Map<String, Object> params) {
        int num = getCommonDao().namingUpdate("sql_svc_pkg_addSvcPkg", params);
        LOG.info(num + " svcpkg(s) added!");
        return num > 0 ? true : false;
    }

    /**
     * 删除套餐和服务关系
     * 
     * @param params 删除条件
     */
    public void delPkgItem(Map<String, Object> params) {
        int num = getCommonDao().namingUpdate(
                "sql_svc_pkg_delSvcPkgItemBySpid", params);
        LOG.info(num + " svcpkgitem(s) deleted!");
    }

    /**
     * 添加套餐和服务关系
     * 
     * @param params 添加值
     */
    public void addPkgItem(Map<String, Object> params) {
        int num = getCommonDao().namingUpdate("sql_svc_pkg_addSvcpkgitem",
                params);
        LOG.info(num + " svcpkgitem(s) added!");
    }

    /**
     * 删除套餐
     * 
     * @param params spid
     * @return void
     */
    public void delpkg(Map<String, Object> params) {
        int num = getCommonDao().namingUpdate("sql_svc_pkg_delSvcPkgBySpid",
                params);
        LOG.info(num + " svcpkgitem(s) deled!");
    }

    /**
     * 添加套餐并保存套餐与套餐项关系
     * 
     * @param params params包含套餐及套餐项信息
     */
    public void addPkgAndRelation(Map<String, Object> params) {
        doAddPkgs(params);
        addPkgItems(params);
    }

    /**
     * 更新套餐并更新套餐与套餐项关系
     * 
     * @param params params包含套餐及套餐项信息
     */
    public void savePkgAndRelation(Map<String, Object> params) {
        doUpdatePkgs(params);
        delPkgItem(params);
        addPkgItems(params);
    }

    /**
     * 删除套餐及套餐项关系
     * 
     * @param params spid
     * @return
     */
    public boolean delpkgAndRel(Map<String, Object> params) {
        try {
            delpkg(params);
            delPkgItem(params);
        } catch (Exception e) {
            LOG.info("delete pkg false, the params is: " + params.toString());
            return false;
        }
        return true;
    }

    /**
     * 批量添加套餐与服务项关系
     * 
     * @param params Map存放key为 spids,svc_item_id字符串，用逗号隔开
     * @return
     */
    public void addPkgItems(Map<String, Object> params) {
        Map<String, Object> addMap;
        String spid = "";
        String[] svc_item_idArray;
        if (null != params) {
            spid = params.get("spid").toString();
            svc_item_idArray = params.get("svc_item_ids").toString().split(",");
        } else {
            LOG.info("0 svcpkgitem(s) added!");
            return;
        }
        for (String svc_item_id : svc_item_idArray) {
            if ("" != svc_item_id) {
                addMap = new HashMap<String, Object>();
                addMap.put("spid", spid);
                addMap.put("svc_item_id", svc_item_id);
                addPkgItem(addMap);
            }
        }
    }
}
